package com.own.component.util.excel.util.write.record;

import com.own.component.util.excel.util.write.function.WriteFormatFunction;
import com.own.component.util.excel.util.write.model.Style;
import com.own.component.util.excel.util.write.style.css.ExcelCssAttribute;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * ExcelItem excel单元格类
 *
 * @author chenxueli
 * @date 2022-06-15 03:14:15
 */
@Getter
public final class CellRecord implements Serializable {

    /**
     * 坐标X
     */
    private final int x;

    /**
     * 坐标Y
     */
    private final int y;

    /**
     * 具体的内容
     */
    @Setter
    private String value;

    /**
     * 单元格样式（初始化为列表中的样式）
     */
    @Setter
    private Style style;

    /**
     * 新增的单独样式表
     */
    @Setter
    private List<ExcelCssAttribute> css;

    /**
     * 批注
     */
    @Setter
    private String comment;

    /**
     * 公式数据
     */
    @Setter
    private String formula;

    /**
     * 数据格式信息
     */
    @Setter
    private WriteFormatFunction dataFormat;

    public CellRecord(int x, int y, String value, Style style) {
        this.x = x - 1;
        this.y = y - 1;
        this.value = value;
        this.style = style;
        this.css = new ArrayList<>();
    }

    public void addCss(ExcelCssAttribute... attribute) {
        css.addAll(Arrays.asList(attribute));
    }

    /**
     * 自定义获取样式的方法
     *
     * @return 样式
     */
    public Style getStyle() {
        if (style == null) {
            if (this.css.isEmpty()) {
                return null;
            }
            style = Style.builder().build();
        }
        if (!this.css.isEmpty()) {
            this.css.forEach(attribute -> attribute.build(style));
        }
        return style;
    }

}
